package jbase.project.system.net.mail;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import jbase.project.system.exception.SysException;
import static jbase.project.system.net.mail.Utils.*;

public class MailService {

	private Session smtpSession;

	public Session getSmtpSession() {
		return smtpSession;
	}

	public void setSmtpSession(Session session) {
		smtpSession = session;
	}

	public void connectSmtpServer(String user, String passwd, String smtpHost, boolean ssl) {
		String[] hosts = smtpHost.split(":");
		String host = hosts[0];
		int port = 25;
		if (hosts.length > 1) {
			port = Integer.parseInt(hosts[1]);
		}
		Properties properties = new Properties();
		properties.setProperty("mail.transport.protocol", "smtp");
		properties.put("mail.smtp.host", host);
		properties.put("mail.smtp.auth", "true");
		properties.put("mail.smtp.port", String.valueOf(port));
		if (ssl) {
			properties.put("mail.smtp.starttls.enable", "true");
		}
		smtpSession = Session.getInstance(properties, new UserPasswdAuthenticator(user, passwd));
	}

	public boolean sendMail(EmailUser from, List<EmailUser> to, List<EmailUser> cc, List<EmailUser> bcc, String subject,
			String body) throws SysException {
		LinkedList<EmailUser> users = new LinkedList<>();
		users.addAll(to);
		if (smtpSession == null) {
			throw new SysException("无活动的SMTP服务器连接");
		} else {
			MimeMessage message = new MimeMessage(smtpSession);
			try {
				message.setFrom(new InternetAddress(from.getEmail(), from.getName(), "UTF-8"));
				EmailUser user = users.removeFirst();
				message.setRecipient(MimeMessage.RecipientType.TO,
						new InternetAddress(user.getEmail(), user.getName(), "UTF-8"));
				for (int i = 0, size = users.size(); i < size; i++) {
					message.addRecipient(MimeMessage.RecipientType.TO,
							new InternetAddress(users.get(i).getEmail(), users.get(i).getName(), "UTF-8"));
				}
				for (int i = 0, size = cc.size(); i < size; i++) {
					message.setRecipient(MimeMessage.RecipientType.CC,
							new InternetAddress(cc.get(i).getEmail(), cc.get(i).getName(), "UTF-8"));
				}
				for (int i = 0, size = bcc.size(); i < size; i++) {
					message.setRecipient(MimeMessage.RecipientType.BCC,
							new InternetAddress(bcc.get(i).getEmail(), bcc.get(i).getName(), "UTF-8"));
				}
				message.setSubject(subject);
				message.setContent(body, "text/html;charset=UTF-8");
				message.setSentDate(new Date());
				message.saveChanges();
				Transport.send(message, message.getAllRecipients());
			} catch (Throwable e) {
				throw new SysException(e);
			}
		}
		return false;
	}

	public static class UserPasswdAuthenticator extends Authenticator {

		protected String user;
		protected String passwd;

		public UserPasswdAuthenticator(String user, String passwd) {
			this.user = user;
			this.passwd = passwd;
		}

		protected PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(user, passwd);
		}
	}

	public static void main(String[] as) throws SysException {
		EmailUser user = user("sleechengn@163.com");
		String account = "sleechengn@163.com";
		String passwd = "";
		String smtpHost = "smtp.163.com:25";
		Boolean ssl = false;
		MailService mailSrv = new MailService();
		mailSrv.connectSmtpServer(account, passwd, smtpHost, ssl);
		mailSrv.sendMail(user, users(user("474483925@qq.com")), users(), users(), "来自Sleechegn开发的JBase程序", "JBase　你好");
	}
}
